<?php

namespace paper\component;

use paper\exception\HttpException;
use paper\http\response\ClosureResponse;
use paper\http\response\Response;
use paper\tpl\ExceptionTpl;

class ExceptionCatcher implements \paper\constraint\ExceptionCatcher
{
    public function onCatch(\Throwable $exception): Response
    {
        $httpCode = 500;
        if ($exception instanceof HttpException)
            $httpCode = $exception->getCode();
        return new ClosureResponse(function () use ($exception) {
            $tpl = new ExceptionTpl($exception);
            $tpl->run();
        }, $httpCode);
    }
}